<?php 
/*
 * grab rss item in different categories.
 *
*/
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE );

ini_set('max_execution_time', 7200);

require_once("../config/constant.php");
require_once(DAEMONE_HOME."/lib/lib_rss_reader.php");
require_once(DAEMONE_HOME."/lib/lib_database.php");
require_once(DAEMONE_HOME."/lib/lib_rss_database.php");

$rss_new_channel_counter = 0;

$rss_total_channel_counter = 0;

while(true)
{
    $result = get_rss_feed_source('WHERE grabbed_status = 0 ', 'LIMIT '.$config['rss']['source.process.limit']);
    if($result === FALSE || mysql_num_rows($result) == 0) 
    {
        echo 'RSS Feed Source : No more records.';
        exit;
    }

    // iterate rss feed url by categories
    while ($row = mysql_fetch_assoc($result)) 
    {
        $rss_channel_total_item_counter = 0;
		$rss_channel_new_item_counter = 0;
        $rss_feed_source_id = $row['rss_feed_source_id'];
        $rss_feed_source_url = $row['rss_feed_source_url'];
        $rss_feed_source_category_id = $row['category_id'];
        $rss_feed_source_category_name = $row['category_name'];
        $rss_feed_source_parent_category_name = $row['parent_category_name'];
        

        echo 'rss_feed_source_url=', $rss_feed_source_url, '<br/>';
        
        $channels = RSS_Retrieve($rss_feed_source_url);
        if ($channels === FALSE)
        {
            continue;
        }
        // populate channels data in db.
        foreach ($channels as &$channel_items) 
        {
            $rss_total_channel_counter ++;
            
            $channel_id = 0;
            foreach ($channel_items as &$item) 
            {
                if($item['type'] == 0) 
                {
                    $item['rss_feed_source_id'] = $rss_feed_source_id;
                    $item['category_id'] = $rss_feed_source_category_id;
                    
                    $channel_id = add_rss_channel($item);
                    // channel item already exist get channel id
                    if($channel_id == 0) 
                    {
                        $channel_result = get_rss_feed_channel(" WHERE link = '".$item['link']."'", '');
                        if(mysql_num_rows($channel_result) > 0)
                        {
                            $channel_row = mysql_fetch_assoc($channel_result);
                            $channel_id = $channel_row['channel_id'];
                        } 
                        else 
                        {
                            // Should not be this case
                            echo 'RSS Channel : channel id not found for ', $item['link'];
                            break;
                        }
                    } 
                    else 
                    {
                        $rss_new_channel_counter++;
                    }
                    
                    $item['channel_id'] = $channel_id;
                    echo 'channel_id = ', $channel_id, '|', "\r\n";
                }
                else 
                {
                    $rss_channel_total_item_counter++;

                    $item['rss_feed_source_id'] = $rss_feed_source_id;
                    $item['category_id'] = $rss_feed_source_category_id;
                    
                    $item['channel_id'] = $channel_id;
                    $item['grabbed_status'] = 0;
                    $item['category_name'] = $rss_feed_source_category_name;
                    $item['parent_category_name'] = $rss_feed_source_parent_category_name;
                    $item_id = add_rss_item($item);
                    if($item_id > 0)
                    {
                        $rss_channel_new_item_counter ++;
                    }
                    // echo 'item_id = ', $item_id, '|', $item['link'], '|', "\r\n";
                }
            }
        }
        
        update_rss_feed_source($rss_feed_source_id, 1);
		$rss_feed_grab_history['rss_feed_source_id'] = $rss_feed_source_id;
		$rss_feed_grab_history['channel_id'] = $channel_id;
		$rss_feed_grab_history['rss_channel_total_item_counter'] = $rss_channel_total_item_counter;	
		$rss_feed_grab_history['rss_channel_new_item_counter'] = $rss_channel_new_item_counter;	
		add_rss_feed_grabbed_history($rss_feed_grab_history);
    }
    
    update_rss_item_pub_date(' WHERE grabbed_status=0 ');
}

?>
